import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'AnimatedSlide Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: AnimatedSlideDemo(),
    );
  }
}

class AnimatedSlideDemo extends StatefulWidget {
  @override
  _AnimatedSlideDemoState createState() => _AnimatedSlideDemoState();
}

class _AnimatedSlideDemoState extends State<AnimatedSlideDemo> {
  // 初始化偏移量
  // double _offset = 0.0;
  Offset offset = Offset.zero;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AnimatedSlide Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 使用 AnimatedSlide 来控制动画效果
            AnimatedSlide(
              offset: offset, // 通过 offset 控制滑动的距离
              duration: Duration(seconds: 1), // 动画持续时间
              child: Container(
                width: 100,
                height: 100,
                color: Colors.blue,
                child: Center(
                  child: Text(
                    'Slide Me!',
                    style: TextStyle(color: Colors.white, fontSize: 20),
                  ),
                ),
              ),
            ),
            SizedBox(height: 30),
            // 按钮1: 设置容器位置为0
            ElevatedButton(
              onPressed: () {
                setState(() {
                  offset = Offset(offset.dx+1,offset.dy);
                  // _offset = 0.0; // 设置位置为0
                });
              },
              child: Text('Right'),
            ),
            SizedBox(height: 10),
            // 按钮2: 设置容器位置为100
            ElevatedButton(
              onPressed: () {
                setState(() {
                  offset = Offset(offset.dx-1,offset.dy);
                });
              },
              child: Text('Left'),
            ),
          ],
        ),
      ),
    );
  }
}
